package com.kinyx.framework.db.daos;

import java.util.List;

import javax.util.XArrayList;

import org.junit.Assert;
import org.junit.Test;

import com.kinyx.framework.db.sentence.str.SQLBatch;
import com.kinyx.framework.enums.impl.UsingStatus;
import com.kinyx.framework.utils.KDateTimeUtils;

public abstract class JDBCDao_Tester_batchChange extends SUPER {

	private String id1 = "1";
	private String id2 = "2";
	private String id3 = "3";
	private String id1_ = "11";
	private String id2_ = "22";
	private String id3_ = "33";

	@SuppressWarnings("deprecation")
	@Test
	public void testSuccess1() {
		this.logger.debug("batchInsert using String");
		final int cnt1 = this.dao.batchInsert(null, "INSERT INTO T_DEMO (ID, USING_STATUS, INSERT_TIME, UPDATE_TIME) VALUES (?, ?, ?, ?)", new XArrayList<List<Object>>()//
				.xAdd(new XArrayList<>().xAdd(this.id1).xAdd(UsingStatus.USING).xAdd(KDateTimeUtils.now("yyyyMMddHHmmssSSS")).xAdd(KDateTimeUtils.now("yyyyMMddHHmmssSSS")))//
				.xAdd(new XArrayList<>().xAdd(this.id2).xAdd("USING").xAdd(KDateTimeUtils.now("yyyyMMddHHmmssSSS")).xAdd(KDateTimeUtils.now("yyyyMMddHHmmssSSS")))//
				.xAdd(new XArrayList<>().xAdd(this.id3).xAdd(UsingStatus.USING).xAdd(KDateTimeUtils.now("yyyyMMddHHmmssSSS")).xAdd(KDateTimeUtils.now("yyyyMMddHHmmssSSS")))//
		);
		Assert.assertTrue(cnt1 == 3);

		this.logger.debug("batchUpdate using String");
		final int cnt2 = this.dao.batchUpdate(null, "UPDATE T_DEMO SET ID = ? WHERE ID = ?", new XArrayList<List<Object>>()//
				.xAdd(new XArrayList<>().xAdd(this.id1_).xAdd(this.id1))//
				.xAdd(new XArrayList<>().xAdd(this.id2_).xAdd(this.id2))//
				.xAdd(new XArrayList<>().xAdd(this.id3_).xAdd(this.id3))//
		);
		Assert.assertTrue(cnt2 == 3);

		this.logger.debug("batchDelete using String");
		final int cnt3 = this.dao.batchDelete(null, "DELETE FROM T_DEMO WHERE ID = ?", new XArrayList<List<Object>>()//
				.xAdd(new XArrayList<>().xAdd(this.id1_))//
				.xAdd(new XArrayList<>().xAdd(this.id2_))//
				.xAdd(new XArrayList<>().xAdd(this.id3_))//
		);
		Assert.assertTrue(cnt3 == 3);
	}

	@Test
	public void testSuccess2() {
		final List<String> list = new XArrayList<String>().xAdd(this.id1).xAdd(this.id2).xAdd(this.id3);
		final List<String> listChanged = new XArrayList<String>().xAdd(this.id1_).xAdd(this.id2_).xAdd(this.id3_);

		final SQLBatch insertBatch = new SQLBatch().s("INSERT INTO T_DEMO (ID, USING_STATUS, INSERT_TIME, UPDATE_TIME) VALUES (?, ?, ?, ?)");
		final SQLBatch updateBatch = new SQLBatch().s("UPDATE T_DEMO SET ID = ?");
		final SQLBatch deleteBatch = new SQLBatch().s("DELETE FROM T_DEMO WHERE ID = ?");
		for (int i = 0; i < list.size(); i++) {
			final String param = list.get(i);
			final String paramChanged = listChanged.get(i);
			insertBatch.p(param, i);
			insertBatch.p(UsingStatus.USING, i);
			insertBatch.p(KDateTimeUtils.now("yyyyMMddHHmmssSSS"), i);
			insertBatch.p(KDateTimeUtils.now("yyyyMMddHHmmssSSS"), i);
			if (i == 0) {
				updateBatch.s(" WHERE ID = ?");
			}
			updateBatch.p(paramChanged, i).p(param, i);
			deleteBatch.p(paramChanged, i);
		}

		this.logger.debug("batchInsert using str.SQLBatch");
		final int cnt1 = this.dao.batchInsert(null, insertBatch);
		Assert.assertTrue(cnt1 == 3);
		this.logger.debug("batchUpdate using str.SQLBatch");
		final int cnt2 = this.dao.batchUpdate(null, updateBatch);
		Assert.assertTrue(cnt2 == 3);
		this.logger.debug("batchDelete using str.SQLBatch");
		final int cnt3 = this.dao.batchDelete(null, deleteBatch);
		Assert.assertTrue(cnt3 == 3);
	}

}
